<?php
namespace BoxAdmin\Controller;

use Think\Controller;

class GroupController extends CommonController {
	/**
	 * [index 桌面首页]
	 * @return [type] [description]
	 */
	public function _initialize() {
		parent::_initialize();
		$this->group = BoxModel("sns_group");
		$this->topic = BoxModel("SnsTopic");
		$this->comment = BoxModel("SnsComment");
		// $this->group_url="http://".$this->store_info['domain'].".snsm.".DomainName."/"	;
		$group_type = dict('group_type');
		$group_url = dict('group_url');
		foreach ($group_type as $k => $one) {
			//$this->admin_store_info['domain']."."
			$group_url[$k] = "http://" . $group_url[$k] . "." . DomainName;
		}
		$this->group_url = $group_url;
	}

	/******所有群组***/
	public function index() {
		$where = 'is_show in (1)';
		$kw = I('kw', '', 'trim');
		if (!$isSpuer) {
			$where .= " and store_id='$this->store_id'";
		}
		if ($kw) {
			$where .= " and (group_name like '%$kw%' or info like '%$kw%')";
		}

		$data = BoxModel("SnsGroup")->get($where, 10);
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->display();
	}

	/****创建群组**/
	public function add() {
		$group_id = I('group_id');
		if ($group_id) {
			$this->data = $this->group->load($group_id);
		}
		$this->group_type = dict('group_type');
		if (IS_POST) {
			$arr = $this->group->create();
			$arr['store_id'] = $this->store_id;
			$arr['user_id'] = intval($arr['user_id']);
			$info = $this->_upload("/group/");
			if (is_array($info)) {
				$arr['picname'] = $info['picname']['savepath'] . $info['picname']['savename'];
			}
			$id = $this->group->create_group($arr);
			if ($id) {
				$this->jump("保存成功", U('Group/add', array('group_id' => $id)));
			}
		}
		$this->display();
	}

	/******删除一个群组******/
	function del_group() {
		$group_id = I('group_id');
		$id = $this->group->del_group($group_id);
		if ($id) {
			$this->jump("操作成功");
		}
	}

	/*****群组成员***/
	function member() {
		$this->memuNav = "index";
		$group_id = I('group_id');
		$this->group_data = BoxModel("sns_group")->load($group_id);
		$this->display();
	}

	/****群组里的帖子**/
	function topic() {
		$group_id = I('group_id');
		$type = I('type');
		$where = "is_show =1 and type='$type'";
		if ($group_id) {
			$where .= " and pid='$group_id'";
		}
		if ($group_id) {
			$this->groupData = $this->group->load($group_id);
			$this->pidName = $this->groupData['group_name'];
			$this->pid = $this->groupData['id'];
		}
		if (!$isSpuer) {
			$where .= " and store_id='$this->store_id'";
		}
		if ($this->kw = I('kw')) {
			$where .= " and title like '%$this->kw%'";
		}
		$this->type = I('type');
		$data = $this->topic->get($where, 10);
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->display();
	}

	/****创建帖子**/
	function add_topic() {
		$this->memuNav = "topic";
		$topic_id = I('topic_id');
		$group_id = I('group_id');
		if ($topic_id) {
			$this->data = $this->topic->load($topic_id);
			$this->pidName = $this->topicData['pidName'];
			$group_id = $this->pid = $this->data['pid'];
		}
		if ($group_id) {
			$this->groupData = $this->group->load($group_id);
			$this->pidName = $this->groupData['group_name'];
			$this->pid = $this->groupData['id'];
			$this->type = $this->groupData['type'];
		}
		if (IS_POST) {
			$data = $this->topic->create();

			$data['content'] = strip_tags(htmlspecialchars_decode(I('content')), dict('allow_tags')); //防止标签转义
			$data['content2'] = strip_tags(htmlspecialchars_decode(I('content2')), dict('allow_tags')); //防止标签转义
			$data['user_id'] = I("user_id", "0", "intval");
			$data['type'] = I('type');
			$data['cate_id'] = I('hide_cate_id', "0", "intval");
			$data['from'] = "webadmin";
			$topic_id = $this->topic->create_topic($data);
			// dump($topic_id);exit();
			if ($topic_id) {
				$this->jump("保存成功");
			}
		}
		$this->display();
	}

	/********删除帖子***/
	function del_topic() {
		$ids = I('ids');
		$ids = explode(",", $ids);
		foreach ($ids as $topic_id) {
			BoxModel("SnsTopic")->del($topic_id);
		}
		$this->jump("删除成功");
	}

	/****帖子里的回复**/
	function comment() {
		$where = "1";
		if ($topic_id = I('topic_id')) {
			$where .= " and pid='$topic_id'";
		}
		$this->type = I('type');
		if ($topic_id) {
			$this->pidData = $this->topic->load($topic_id);
			$this->type = $this->pidData['type'];
		}
		if ($kw = I('kw')) {
			$where .= " and (title like '%$kw%')";
		}

		$data = BoxModel('SnsComment')->get($this->type, $where, 10);
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->display();
	}

	/*********添加评论***/
	function add_comment() {
		$topic_id = I('topic_id');
		$this->type = I('type');
		if ($topic_id) {
			$topic_data = $this->topic->load($topic_id);
			$this->pid = $topic_data['topic_id'];
			$this->pname = $topic_data['title'];
		}
		if ($comment_id = I('comment_id')) {
			$data = $this->comment->load($this->type, $comment_id);
			$this->pid = $data['pid'];
			$topic_data = $this->topic->load($this->pid);
			$this->data = $data;
		}
		$this->topic_data = $topic_data;
		//dump($topic_data);
		if (IS_POST) {
			$row = $this->comment->create();
			$row['type'] = $this->type;
			$r = $this->comment->create_comment($row);
			if ($r) {
				$this->jump("保存成功", U('Group/comment', array('topic_id' => $row['pid'])));
			}
		}
		$this->display();
	}

	/********删除回复***/
	function del_comment() {
		$ids = I('ids');
		$ids = explode(",", $ids);
		foreach ($ids as $comment_id) {
			BoxModel("SnsComment")->del($comment_id);
		}
		$this->jump("删除成功");
	}

	/******相册管理*******/
	function images() {
		$this->display();
	}

	/********设置***/
	function setting() {
		if (IS_POST) {
			$comment_type = I('comment_type');
			$topic_type = I('topic_type');
			foreach ($topic_type as $one) {
				$topic_type_array[$one['type']] = $one;
			}

			foreach ($comment_type as $one) {
				$comment_type_array[$one['type']] = $one;
			}

			dict("comment_type", $comment_type_array);
			dict("topic_type", $topic_type_array);
			$this->jump("操作成功");
		}
		$this->comment_type = dict("comment_type");
		$this->topic_type = dict("topic_type");
		$this->display();
	}
	/***set*/
	function set() {
		$act = I('act');
		if ($act == "is_best" || $act == "is_top") {
			$topic_id = I('topic_id');
			$data = BoxModel("sns_topic")->where(array('id' => $topic_id))->find();
			$value = $data[$act] ? 0 : 1;
			$r = BoxModel("sns_topic")->where(array('id' => $topic_id))->save(array($act => $value));
			if ($r !== false) {
				$this->jump("操作成功");
			}
		}
	}

}

?>